package com.ct.gd.passport.view.weather.util;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ct.gd.passport.view.weather.WeatherCondition;

/**
 * 天气xml数据提取类
 * @author Administrator
 *
 */
public class SinaWeatherHandler extends DefaultHandler {
	
	public static final String CITY = "city";
	public static final String STATUS1 = "status1";
	public static final String STATUS2 = "status2";
	public static final String FIGURE1 = "figure1";
	public static final String FIGURE2 = "figure2";
	public static final String TEMPERATURE1 = "temperature1";
	public static final String TEMPERATURE2 = "temperature2";
	public static final String SAVE_DATE = "savedate_weather";
	
	private WeatherCondition mWeatherCondition;
	private StringBuffer buf= new StringBuffer();
	
	public WeatherCondition getWeatherCondition(){
		return mWeatherCondition;
	}
	
	@Override
	public void startDocument() throws SAXException {
		mWeatherCondition = new WeatherCondition();
		buf.setLength(0);
	}
	
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		//Log.i("startElement", localName);
		buf.setLength(0);
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		buf.append(ch,0,length-start);
		//Log.i("characters", buf.toString());
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		//Log.i("endElement", localName);
		if(buf.length()>0){
			String value = buf.toString();
			if(CITY.equals(localName)){
				mWeatherCondition.city = value;
			}else if(STATUS1.equals(localName)){
				mWeatherCondition.status1 = value;
			}else if(STATUS2.equals(localName)){
				mWeatherCondition.status2 = value;
			}else if(FIGURE1.equals(localName)){
				mWeatherCondition.figure1 = value;
			}else if(FIGURE2.equals(localName)){
				mWeatherCondition.figure2 = value;
			}else if(TEMPERATURE1.equals(localName)){
				mWeatherCondition.temperature1 = getTemperature(value);
			}else if(TEMPERATURE2.equals(localName)){
				mWeatherCondition.temperature2 = getTemperature(value);
			}else if(SAVE_DATE.equals(localName)){
				mWeatherCondition.date = value;
				mWeatherCondition.setShortDate(value);
			}
			buf.setLength(0);
		}
	}
	
	private int getTemperature(String value){
		if(value.length()>0){
			return Integer.parseInt(value);
		}else{
			return Integer.MIN_VALUE;
		}
	}
	
}
